package com.webapp.model;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class PageModel {

	private Statement stmt = DatabaseConnector.connect();
	private int size = 0;
	private ArrayList<Page> pages;
	
	public boolean createNewPage(int pageId, int limit, int totalResults, int postCount, int circle){
		Page page = new Page();
		page.setPageId(pageId);
		page.setItemLimit(limit);
		page.setNumResult(totalResults);
		page.setPostCount(postCount);
		page.setAssociatedCircle(circle);
		
		return true;
	}
	
	/**
	 * Method that connects to the database and gathers the data for a specific page
	 * @param pageId the ID for the desired post
	 * @return the page that was requested
	 */
	public Page getPage(int pageId){
		ResultSet rs;
		try{
			rs = stmt.executeQuery("SELECT * FROM page WHERE Page_Id = '" +pageId+ "'");
			if(rs.next() != false){
				Page page = new Page();
				page.setPageId(pageId);
				page.setPostCount(rs.getInt(2));
				page.setAssociatedCircle(rs.getInt(3));
				
				rs.close();
				return page;
			}
			else
				return null;
		}
		catch(Exception ex){return null;}
	}
	
	/**
	 * Method that connects to the database and inserts a new Page into the 'page' table
	 * @param newPage the page to be added
	 * @return returns TRUE of successful, otherwise FALSE
	 */
	public boolean createPage(Page newPage){
		try{
			stmt.executeQuery("INSERT INTO page (PageID, PostCount, CircleID) VALUES ('"+
					newPage.getPageId()+ "', '" +newPage.getPostCount()+ "', '" +newPage.getAssociatedCircle()+"')");
		}
		catch(Exception ex){return false;}
		return true;
	}
	
	/**
	 * Method that connects to the database and removes an existing page
	 * @param pageId the ID to the post that will be deleted
	 * @return returns TRUE if successful, otherwise false
	 */
	public boolean deletePage(int pageId){
		try{
			stmt.executeQuery("DELETE FROM page WHERE PageID = '" +pageId+ "'");
		}catch(Exception ex){return false;}
		return true;
	}
}

